python min key

34

square = {2: 4, 3: 9, -1: 1, -2: 4}

# the smallest key
key1 = min(square)
print("The smallest key:", key1)    # -2

# the key whose value is the smallest
key2 = min(square, key = lambda k: square[k])

print("The key with the smallest value:", key2)    # -1

# getting the smallest value
print("The smallest value:", square[key2])    # 1

#OUTPUT
#The smallest key: -2
#The key with the smallest value: -1
#The smallest value: 1

Comments

Submit
0 Comments